package com.lijian.client.controller;

import com.lijian.client.feign.OrderFeign;
import com.lijian.pojo.Order;
import com.lijian.pojo.OrderResult;
import com.lijian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/order")
public class OrderClientController {

    @Autowired
    private OrderFeign orderFeign;

    @GetMapping("/save/{menuId}")
    public String createOrder(@PathVariable Integer menuId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Order order = new Order();
        order.setCreateTime(new Date());
        order.setUserId(user.getId());
        order.setMenuId(menuId);
        order.setState(0);
        orderFeign.save(order);
        return "order";
    }

    @GetMapping("/findAllByUid")
    @ResponseBody
    public OrderResult findAllByUId(@RequestParam Integer page, @RequestParam Integer limit, HttpSession session) {
        User user = (User) session.getAttribute("user");
        return orderFeign.findAllByUid(page, limit, user.getId());
    }

    @GetMapping("/list")
    public String orderList(){
        return "order";
    }

    @GetMapping("index")
    public String index(){
        return "index";
    }

    @GetMapping("/orderHandler")
    public String orderHandler(){
        return "/order_handler";
    }

    @GetMapping("/findAllByState")
    @ResponseBody
    public OrderResult findAllByState(@RequestParam Integer page, @RequestParam Integer limit){
        return orderFeign.findAllByState(page, limit);
    }

    @GetMapping("/updateState/{id}")
    public String updateState(@PathVariable Integer id){
        orderFeign.update(id);
        return "redirect:/order/orderHandler";
    }

}
